package com.niiwoo.civet.trade.service.local.loan.submit;

import com.alibaba.dubbo.config.annotation.Reference;
import com.niiwoo.activity.stage.service.inviterBuddy.InviteBuddyActivityDubboService;
import com.niiwoo.activity.stage.service.prize.ActivityPrizeDubboService;
import com.niiwoo.asset.service.manager.R360LoanCommonDubboService;
import com.niiwoo.civet.account.service.AccountDubboService;
import com.niiwoo.civet.trade.enums.LoanSubmitTypeEnum;
import com.niiwoo.civet.trade.service.databackflow.TianChengDataBackFlowDubboService;
import com.niiwoo.civet.user.service.UserBasicInfoDubboService;
import com.niiwoo.civet.user.service.UserDataDubboService;
import com.niiwoo.civet.user.service.UserStatusDubboService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @Author: wuke
 * @Description:
 * @Date: Created in 15:46 2018/4/14
 */
@Component
public class LoanSubmitServiceFactory implements ApplicationContextAware {
    private ApplicationContext applicationContext;
    @Reference(version = "1.0.0")
    private UserDataDubboService userDataDubboService;
    @Reference(version = "1.0.0")
    private ActivityPrizeDubboService activityPrizeDubboService;
    @Reference(version = "1.0.0")
    private UserStatusDubboService userStatusDubboService;
    @Reference(version = "1.0.0")
    private UserBasicInfoDubboService userBasicInfoDubboService;
    @Reference(version = "1.0.0")
    private InviteBuddyActivityDubboService inviteBuddyActivityDubboService;
    @Reference(version = "1.0.0")
    private TianChengDataBackFlowDubboService tianChengDataBackFlowDubboService;
    @Reference(version = "1.0.0")
    private R360LoanCommonDubboService r360LoanCommonDubboService;
    @Reference(version = "1.0.0")
    private AccountDubboService accountDubboService;

    private Map<LoanSubmitTypeEnum,AbstractLoanSubmitService> LOAN_SUBMIT_SERVICE_MAP  = new HashMap<>();

    public AbstractLoanSubmitService getLoanSubmitService(LoanSubmitTypeEnum loanSubmitTypeEnum){
        return LOAN_SUBMIT_SERVICE_MAP.get(loanSubmitTypeEnum);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @PostConstruct
    public void initLoanSubmitService(){
        Optional.ofNullable(applicationContext.getBeansOfType(AbstractLoanSubmitService.class))
                .map(Map::values)
                .ifPresent(list->{
                    list.forEach(l->{
                        l.setUserDataDubboService(userDataDubboService);
                        l.setActivityPrizeDubboService(activityPrizeDubboService);
                        l.setUserStatusDubboService(userStatusDubboService);
                        l.setUserBasicInfoDubboService(userBasicInfoDubboService);
                        l.setInviteBuddyActivityDubboService(inviteBuddyActivityDubboService);
                        l.setTianChengDataBackFlowDubboService(tianChengDataBackFlowDubboService);
                        l.setR360LoanCommonDubboService(r360LoanCommonDubboService);
                        l.setAccountDubboService(accountDubboService);
                        LOAN_SUBMIT_SERVICE_MAP.put(l.getLoanSubmitMapEnum(),l);
                    });
                });
    }
}
